import pygame

import sys

import random

import numpy

back_ground_1_x=0

back_ground_1_y=0

class bomb_role(object):

    #创建一个类用来添加控制角色

    def __init__(self):

        #初始化这个角色需要的控制参数

        self.start=False

        self.bomb_status=[pygame.image.load("bomb_status1.png").convert_alpha(),pygame.image.load("bomb_status2.png").convert_alpha(),

                          pygame.image.load("bomb_status3.png").convert_alpha(),pygame.image.load("bomb_status4.png").convert_alpha(),

                          pygame.image.load("bomb_status5.png").convert_alpha(),pygame.image.load("bomb_status6.png").convert_alpha(),

                          pygame.image.load("bomb_status7.png").convert_alpha(),pygame.image.load("bomb_status8.png").convert_alpha()]

        self.bomb_rect=[self.bomb_status[0].get_rect(),self.bomb_status[1].get_rect(),self.bomb_status[2].get_rect(),

                        self.bomb_status[3].get_rect(),self.bomb_status[4].get_rect(),self.bomb_status[5].get_rect(),

                        self.bomb_status[6].get_rect(),self.bomb_status[7].get_rect()]
        self.bomb_count=0

    def bomb_update(self,count):

        if self.start==True:
            bomb_speed=5

            self.bomb_count=self.bomb_count+1  ##控制炸弹刷新的速度

            Delta_role_x=numpy.random.randint(-50,100)

            Delta_role_y=numpy.random.randint(-50,100)

            role_Random_num=random.Random()

            init_role_x=role_Random_num.randint(numpy.minimum(Plane.plane_rect.x-Plane.Delta_x,Plane.plane_rect.x+Plane.Delta_x),numpy.maximum(Plane.plane_rect.x-Plane.Delta_x,Plane.plane_rect.x+Plane.Delta_x))

            init_role_y=role_Random_num.randint(numpy.minimum(Plane.plane_rect.y-Plane.Delta_y,Plane.plane_rect.y+Plane.Delta_y),numpy.maximum(Plane.plane_rect.y-Plane.Delta_y,Plane.plane_rect.y+Plane.Delta_y))

            self.bomb_rect[0].bottomright=(init_role_x,init_role_y)

            back_ground_1_x=back_ground_1_rect.x

            back_ground_1_y=back_ground_1_rect.y

            back_ground_1_rect.topleft=(back_ground_1_x,back_ground_1_y)#这要给加载的图片位置赋值，以保证后面能使中这个值

            if self.bomb_count<=0.5*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                screen.blit(self.bomb_status[0],self.bomb_rect[0].center)
                pygame.display.update(self.bomb_rect[0].inflate(150,150))#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
            elif self.bomb_count<=2*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                self.bomb_rect[1].center=(self.bomb_rect[0].centerx+Delta_role_x,self.bomb_rect[0].centery+Delta_role_y)
                screen.blit(self.bomb_status[1],self.bomb_rect[1].center)
                pygame.display.update(self.bomb_rect[1].inflate(100,100))#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
            elif self.bomb_count<=3*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                self.bomb_rect[2].center=(self.bomb_rect[1].centerx+Delta_role_x,self.bomb_rect[1].centery+Delta_role_y)
                screen.blit(self.bomb_status[2],self.bomb_rect[2].center)
                pygame.display.update(self.bomb_rect[2].inflate(100,100))#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
            elif self.bomb_count<=4*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                self.bomb_rect[3].center=(self.bomb_rect[2].centerx+Delta_role_x,self.bomb_rect[2].centery+Delta_role_y)
                screen.blit(self.bomb_status[3],self.bomb_rect[3].center)
                pygame.display.update(self.bomb_rect[3].inflate(100,100))#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
            elif self.bomb_count<=5*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                Delta_role_x=Delta_role_x/10
                Delta_role_y=Delta_role_y/10
                self.bomb_rect[4].center=(self.bomb_rect[3].centerx+Delta_role_x,self.bomb_rect[3].centery+Delta_role_y)
                screen.blit(self.bomb_status[4],self.bomb_rect[4].center)
                pygame.display.update(self.bomb_rect[4].inflate(100,100))#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
                Keng.keng_rect[0].topleft=self.bomb_rect[4].center
                screen.blit(Keng.keng_case[0],Keng.keng_rect[0].center)
                pygame.display.update(Keng.keng_rect[0])#这个很重要，没有会黑屏
                
            elif self.bomb_count<=6*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                Delta_role_x=Delta_role_x/10
                Delta_role_y=Delta_role_y/10
                self.bomb_rect[5].center=(self.bomb_rect[4].centerx+Delta_role_x,self.bomb_rect[4].centery+Delta_role_y)
                screen.blit(self.bomb_status[5],self.bomb_rect[5].center)
                pygame.display.update(self.bomb_rect[5].inflate(100,100))#这个很重要，没有会黑屏
                
                Keng.keng_rect[1].topleft=self.bomb_rect[5].center
                screen.blit(Keng.keng_case[1],Keng.keng_rect[1].center)
                pygame.display.update(Keng.keng_rect[1])#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
            elif self.bomb_count<=7*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                Delta_role_x=Delta_role_x/10
                Delta_role_y=Delta_role_y/10
                self.bomb_rect[6].center=(self.bomb_rect[5].centerx+Delta_role_x,self.bomb_rect[5].centery+Delta_role_y)
                screen.blit(self.bomb_status[6],self.bomb_rect[6].center)
                pygame.display.update(self.bomb_rect[6].inflate(100,100))#这个很重要，没有会黑屏
                
                Keng.keng_rect[2].topleft=self.bomb_rect[6].center
                screen.blit(Keng.keng_case[2],Keng.keng_rect[2].center)
                pygame.display.update(Keng.keng_rect[2])#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                
            elif self.bomb_count<=8*bomb_speed:
                screen.blit(back_ground_1,(back_ground_1_x,back_ground_1_y))
                
                Delta_role_x=Delta_role_x/10
                Delta_role_y=Delta_role_y/10
                self.bomb_rect[7].center=(self.bomb_rect[6].centerx+Delta_role_x,self.bomb_rect[6].centery+Delta_role_y)
                screen.blit(self.bomb_status[7],self.bomb_rect[7].center)
                pygame.display.update(self.bomb_rect[7].inflate(100,100))#这个很重要，没有会黑屏
                
                Keng.keng_rect[3].topleft=self.bomb_rect[7].center
                screen.blit(Keng.keng_case[3],Keng.keng_rect[3].center)
                pygame.display.update(Keng.keng_rect[3])#这个很重要，没有会黑屏
                
                screen.blit(Plane.plane_status,Plane.plane_rect)
                pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
                Role.start=False
                self.bomb_count=0

            ##pygame.time.delay(20)
            

class plane_role(object):

    def __init__(self):

        #初始化这个角色需要的控制参数

        self.Plane_case=[pygame.image.load("plane_status1.png").convert_alpha(),pygame.image.load("plane_status2.png").convert_alpha(),

                         pygame.image.load("plane_status3.png").convert_alpha(),pygame.image.load("plane_status4.png").convert_alpha()]

        self.plane_status=self.Plane_case[3]

        self.plane_rect=self.plane_status.get_rect()

        self.plane_rect.topleft=(400,320)#(screen.get_width()/2,screen.get_height()/2)

        screen.blit(self.plane_status,self.plane_rect) #窗口上要添加的图片内容，先添加最后层

        pygame.display.update(self.plane_rect)#这个很重要，没有会黑屏

        self.Delta_x=0

        self.Delta_y=0

    def Plane_place(self,Delta_x,Delta_y):

        self.Delta_x=Delta_x

        self.Delta_y=Delta_y

        plane_x=self.plane_rect.x

        plane_y=self.plane_rect.y

        plane_x=plane_x+Delta_x

        plane_y=plane_y+Delta_y

        self.plane_rect.topleft=(plane_x,plane_y)

        screen.blit(self.plane_status,self.plane_rect) #窗口上要添加的图片内容，先添加最后层

        pygame.display.update(self.plane_rect)#这个很重要，没有会黑屏

        Role.start=True
        
        bomb_role.bomb_count=0

class keng(object):

    def __init__(self):

        self.keng_case=[pygame.image.load("keng1.png").convert_alpha(),pygame.image.load("keng2.png").convert_alpha(),

                        pygame.image.load("keng3.png").convert_alpha(),pygame.image.load("keng4.png").convert_alpha()]

        self.keng_rect=[self.keng_case[0].get_rect(),self.keng_case[1].get_rect(),

                        self.keng_case[2].get_rect(),self.keng_case[3].get_rect()]

        for keng_num in range(0,4):

            self.keng_rect[keng_num].center=(5000,5000)

            screen.blit(self.keng_case[keng_num],self.keng_rect[keng_num]) #窗口上要添加的图片内容，先添加最后层

 

def creat_map(count):

    #定义一个刷新地图的函数。

    screen.fill((255,255,255)) #窗口填充颜色

    ############放置back_ground 1############

    back_ground_1_x=back_ground_1_rect.x

    back_ground_1_y=back_ground_1_rect.y

    for keng_num in range(0,4):

        Keng.keng_rect[keng_num].topleft=(Keng.keng_rect[keng_num].x-back_ground_1_x,Keng.keng_rect[keng_num].y-back_ground_1_y)

   

    if count==1:

        display_center_x=screen.get_width()/2

        display_center_y=screen.get_height()/2

        back_ground_1_x=-100#display_center_x//1-back_ground_1.get_width() // 2

        back_ground_1_y=-250#display_center_y//1-back_ground_1.get_height() // 2

        back_ground_1_rect.topleft=(back_ground_1_x,back_ground_1_y)#这要给加载的图片位置赋值，以保证后面能使中这个值

    else:

        back_ground_1_rect.topleft=(back_ground_1_x-Plane.Delta_x,back_ground_1_y-Plane.Delta_y)#这要给加载的图片位置赋值，以保证后面能使中这个值

 

    screen.blit(back_ground_1,back_ground_1_rect) #窗口上要添加的图片内容，先添加最后层

    
    for keng_num in range(0,4):

        Keng.keng_rect[keng_num].topleft=(Keng.keng_rect[keng_num].x+back_ground_1_rect.x,Keng.keng_rect[keng_num].y+back_ground_1_rect.y)

        screen.blit(Keng.keng_case[keng_num],Keng.keng_rect[keng_num])

        pygame.display.update(Keng.keng_rect[keng_num])#这个很重要，没有会黑屏

    screen.blit(Plane.plane_status,Plane.plane_rect) #窗口上要添加的图片内容，先添加最后层

    pygame.display.update(Plane.plane_rect)#这个很重要，没有会黑屏
    
    ############放置back_ground 1############

    ##添加角色活动

    Role.bomb_update(count)

    pygame.display.update()#这个很重要，没有会黑屏


#主程序如下

if __name__=="__main__":

    pygame.init()

    size=800,800

    screen=pygame.display.set_mode(size, pygame.RESIZABLE)
    pygame.display.set_caption("使用方向键进行控制")

    clock=pygame.time.Clock()

    display_center_x=screen.get_width()/2

    display_center_y=screen.get_height()/2

    back_ground_1=pygame.image.load("back_ground_1.png")

    back_ground_1_rect=back_ground_1.get_rect()

    count=0

    Plane=plane_role()

    Role=bomb_role()

    Keng=keng()

    Delta_key=10
    
    bomb_role.bomb_count=0


    #下边的循环让窗口保持执行下去

    while True:

        clock.tick(30) #循环速度

        count=count+1

        for event in pygame.event.get():

            if event.type==pygame.QUIT:

                pygame.quit()

                sys.exit()

            if event.type == pygame.VIDEORESIZE:#设置可以调节窗口大小

                screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)

                count=1

            if event.type == pygame.KEYDOWN:

               

                if event.key == pygame.K_UP:

                    Plane.plane_status=Plane.Plane_case[0]

                    Plane.Plane_place(0,-1*Delta_key)

                elif event.key == pygame.K_DOWN:

                    Plane.plane_status=Plane.Plane_case[1]

                    Plane.Plane_place(0,1*Delta_key)

                elif event.key == pygame.K_LEFT:

                    Plane.plane_status=Plane.Plane_case[2]

                    Plane.Plane_place(-1*Delta_key,0)

                elif event.key == pygame.K_RIGHT:

                    Plane.plane_status=Plane.Plane_case[3]

                    Plane.Plane_place(1*Delta_key,0)

 

            #if event.type==

            #   XXXXXXXXXXX这里写各种控制来改变类的基本参数

        creat_map(count)#每个循环刷新一次屏幕

